package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.PermissionAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {


    private IDepartmentService service;

    @Autowired
    public void setService(IDepartmentService service) {
        this.service = service;
    }

    @PermissionAnnotation(name = "部门列表",expression = "department.list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageInfo<Department> pageResult = service.getPageInfo(qo);
        model.addAttribute("PageInfo",pageResult);
        return "department/list";
    }

    @PermissionAnnotation(name = "部门删除",expression = "department.delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            service.delete(id);
        }
        return "redirect:/department/list";
    }


    @PermissionAnnotation(name = "部门保存",expression = "department.saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        if (department.getId()!=null){
            service.update(department);
        }else {
            service.insert(department);

        }
        return "redirect:/department/list";
    }
}
